clear all 
set more off
* To replicate figure 1 use data file named "figure_1_replication.dta":
use "Tables_data.dta"

* calculate median voter's position by country:
bysort party_id: gen x=_n
bysort country: egen mean_lr=median(uni_resp_selfplace)
bysort country: egen mean_eu=median(uni_v_9)
bysort country: egen mean_imm=median(uni_v_6)
bysort country: gen xx=_n
*order countries by name:
recode country (0=1 "Belgium (F)") (2=2 "Denmark") ///
(3=5 "Germany")(5=8 "Spain")(6=4 "France")(7=.)(10=7 "Netherlands") ///
(11=10 "United Kingdom") (12=.) (13=0 "Austria")(14=3 "Finland")(16=9 "Sweden") (38=6 "Luxembourg"),gen(c1)
* Figure 1A:
scatter c1 mean_all if group==1 & x==1, msymbol(o) mcol(orange) msize(large)  || ///
scatter c1 mean_all if group==2 & x==1, msymbol(oh) mcol(blue) || ///
scatter c1 mean_all if group==3 & x==1, msymbol(oh) mcol(red) || ///
scatter c1 mean_all if group==4 & x==1, msymbol(oh) mcol(green) || ///
scatter c1 mean_all if group==5 & x==1, msymbol(oh) mcol(brown) || ///
scatter c1 mean_all if group==6 & x==1, msymbol(oh) mcol(black) || ///
scatter c1 mean_lr if xx==1, msymbol(d) mcol(gray) msize(large) ///
ytitle("", c(black) m(r+3)) ///
ylab(0(1)10, valuelabel angle(horizontal) glcolor(gs14))  ///
xtitle("Left-Right Position", c(black)) xlab(0(1)10) ///
graphregion(color(white)) bgcolor(white) legend(off) ///
title("", c(black) j(right)) 
graph save Graph "LR_positions.gph" ,replace 
graph export "LR_positions.png", as(png) replace
* Figure 1B:
scatter c1 pv_6 if group==1 & x==1, msymbol(o) mcol(orange) msize(large)  || ///
scatter c1 pv_6 if group==2 & x==1, msymbol(oh) mcol(blue) || ///
scatter c1 pv_6 if group==3 & x==1, msymbol(oh) mcol(red) || ///
scatter c1 pv_6 if group==4 & x==1, msymbol(oh) mcol(green) || ///
scatter c1 pv_6 if group==5 & x==1, msymbol(oh) mcol(brown) || ///
scatter c1 pv_6 if group==6 & x==1, msymbol(oh) mcol(black) || ///
scatter c1 mean_imm if xx==1, msymbol(d) mcol(gray) msize(large) ///
ytitle("", c(black) m(r+3)) ///
ylab(0(1)10, valuelabel angle(horizontal) glcolor(gs14))  ///
xtitle("Immigration Position", c(black)) xlab(0(1)10) ///
graphregion(color(white)) bgcolor(white) legend(off) ///
title("", c(black) j(right)) 
graph save Graph "Imm_positions.gph" ,replace 
graph export "Imm_positions.png", as(png) replace
* Figure 1C: 
scatter c1 pv_9 if group==1 & x==1, msymbol(o) mcol(orange) msize(large) || ///
scatter c1 pv_9 if group==2 & x==1, msymbol(oh) mcol(blue) || ///
scatter c1 pv_9 if group==3 & x==1, msymbol(oh) mcol(red) || ///
scatter c1 pv_9 if group==4 & x==1, msymbol(oh) mcol(green) || ///
scatter c1 pv_9 if group==5 & x==1, msymbol(oh) mcol(brown) || ///
scatter c1 pv_9 if group==6 & x==1, msymbol(oh) mcol(black) || ///
scatter c1 mean_eu if xx==1, msymbol(d) mcol(gray)  msize(large) ///
ytitle("", c(black) m(r+3)) ///
ylab(0(1)10, valuelabel angle(horizontal) glcolor(gs14))  ///
xtitle("European Integration Position", c(black)) xlab(0(1)10) ///
graphregion(color(white)) bgcolor(white) legend(lab(1 "Center") lab(2 "Major Right") lab(3 "Major Left") lab(4 "Green") lab(5 "Other Left") lab(6 "Other Right") lab(7 "Median Voter") row(3)) ///
title("", c(black) j(right)) 
graph save Graph "EU_positions.gph" ,replace 
graph export "EU_positions.png", as(png) replace

* Combined Figure 1:
graph combine "LR_positions.gph" "Imm_positions.gph" "EU_positions.gph", row(2)   ///
graphregion(color(white)) ysize(8) xsize(10) altshrink commonscheme ycommon

* To replicate figures 2-4 use data file named "simulation_results.dta":
clear all
set more off
use "simulations_results"

graph box gain_uni, over(group)  ///
box(1, col(black)) marker(1, mcol(black) mlab(party_name) mlabcol(black) mlabposition(12)) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right)) ///
ylab(0(5)25)

* Figure 3:
graph box gain, over(group)  ///
box(1, col(black))  marker(1, mcol(black) mlab(party_name) mlabcol(black) mlabposition(12)) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right)) ///
ylab(0(5)25)


* Figre 4:
graph box gain_valence, over(group)  ///
box(1, col(black)) marker(1, mcol(black) mlab(party_name) mlabcol(black) mlabposition(12)) ///
ytitle("Vote Share Increase", c(black) m(r+3)) ylab(,nogrid) ///
graphregion(color(white)) bgcolor(white) ///
title("", c(black) j(right)) ///
ylab(0(5)25)



